package work.xiaohong.puke.controller;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.web.bind.annotation.*;
import work.xiaohong.puke.pojo.Result;
import work.xiaohong.puke.pojo.StatusCode;
import java.util.*;

/**
 * @author : xiaohong
 * @date : 2021/5/30 14:13
 */
@RestController
@RequestMapping("/puke/game")
@CrossOrigin
public class gameController {

    private List<String> sortRule = Arrays.asList(
            "3ht.jpg","3hx.jpg","3mh.jpg","3fk.jpg",
            "4ht.jpg","4hx.jpg","4mh.jpg","4fk.jpg",
            "5ht.jpg","5hx.jpg","5mh.jpg","5fk.jpg",
            "6ht.jpg","6hx.jpg","6mh.jpg","6fk.jpg",
            "7ht.jpg","7hx.jpg","7mh.jpg","7fk.jpg",
            "8ht.jpg","8hx.jpg","8mh.jpg","8fk.jpg",
            "9ht.jpg","9hx.jpg","9mh.jpg","9fk.jpg",
            "10ht.jpg","10hx.jpg","10mh.jpg","10fk.jpg",
            "jht.jpg","jhx.jpg","jmh.jpg","jfk.jpg",
            "qht.jpg","qhx.jpg","qmh.jpg","qfk.jpg",
            "kht.jpg","khx.jpg","kmh.jpg","kfk.jpg",
            "1ht.jpg","1hx.jpg","1mh.jpg","1fk.jpg",
            "2ht.jpg","2hx.jpg","2mh.jpg","2fk.jpg",
            "dw.jpg", "xw.jpg"
    );


    private List OnePuke = new ArrayList<>(Arrays.asList(
            "1ht.jpg",
            "2ht.jpg", "3ht.jpg", "4ht.jpg", "5ht.jpg", "6ht.jpg", "7ht.jpg", "8ht.jpg", "9ht.jpg", "10ht.jpg",
            "jht.jpg", "qht.jpg", "kht.jpg",
            "1hx.jpg", "2hx.jpg", "3hx.jpg", "4hx.jpg", "5hx.jpg", "6hx.jpg", "7hx.jpg", "8hx.jpg", "9hx.jpg", "10hx.jpg",
            "jhx.jpg", "qhx.jpg", "khx.jpg",
            "1mh.jpg", "2mh.jpg", "3mh.jpg", "4mh.jpg", "5mh.jpg", "6mh.jpg", "7mh.jpg", "8mh.jpg", "9mh.jpg", "10mh.jpg",
            "jmh.jpg", "qmh.jpg", "kmh.jpg",
            "1fk.jpg", "2fk.jpg", "3fk.jpg", "4fk.jpg", "5fk.jpg", "6fk.jpg", "7fk.jpg", "8fk.jpg", "9fk.jpg", "10fk.jpg",
            "jfk.jpg", "qfk.jpg", "kfk.jpg",
            "dw.jpg", "xw.jpg"
    ));

    private List TwoPuke = new ArrayList<>(Arrays.asList(
            "1ht.jpg", "2ht.jpg", "3ht.jpg", "4ht.jpg", "5ht.jpg", "6ht.jpg", "7ht.jpg", "8ht.jpg", "9ht.jpg", "10ht.jpg",
            "jht.jpg", "qht.jpg", "kht.jpg",
            "1hx.jpg", "2hx.jpg", "3hx.jpg", "4hx.jpg", "5hx.jpg", "6hx.jpg", "7hx.jpg", "8hx.jpg", "9hx.jpg", "10hx.jpg",
            "jhx.jpg", "qhx.jpg", "khx.jpg",
            "1mh.jpg", "2mh.jpg", "3mh.jpg", "4mh.jpg", "5mh.jpg", "6mh.jpg", "7mh.jpg", "8mh.jpg", "9mh.jpg", "10mh.jpg",
            "jmh.jpg", "qmh.jpg", "kmh.jpg",
            "1fk.jpg", "2fk.jpg", "3fk.jpg", "4fk.jpg", "5fk.jpg", "6fk.jpg", "7fk.jpg", "8fk.jpg", "9fk.jpg", "10fk.jpg",
            "jfk.jpg", "qfk.jpg", "kfk.jpg",
            "dw.jpg", "xw.jpg",
            "1ht.jpg", "2ht.jpg", "3ht.jpg", "4ht.jpg", "5ht.jpg", "6ht.jpg", "7ht.jpg", "8ht.jpg", "9ht.jpg", "10ht.jpg",
            "jht.jpg", "qht.jpg", "kht.jpg",
            "1hx.jpg", "2hx.jpg", "3hx.jpg", "4hx.jpg", "5hx.jpg", "6hx.jpg", "7hx.jpg", "8hx.jpg", "9hx.jpg", "10hx.jpg",
            "jhx.jpg", "qhx.jpg", "khx.jpg",
            "1mh.jpg", "2mh.jpg", "3mh.jpg", "4mh.jpg", "5mh.jpg", "6mh.jpg", "7mh.jpg", "8mh.jpg", "9mh.jpg", "10mh.jpg",
            "jmh.jpg", "qmh.jpg", "kmh.jpg",
            "1fk.jpg", "2fk.jpg", "3fk.jpg", "4fk.jpg", "5fk.jpg", "6fk.jpg", "7fk.jpg", "8fk.jpg", "9fk.jpg", "10fk.jpg",
            "jfk.jpg", "qfk.jpg", "kfk.jpg",
            "dw.jpg", "xw.jpg"
    ));

    // 一副牌开局
    @GetMapping("/start/one/{player}")
    @Parameter(in = ParameterIn.PATH, name = "player", description = "玩家人数", required = true)
    public Result startGameOne(@PathVariable Integer player) {
        List resultList = randomList(OnePuke);
        Map<String, List> resultMap = new HashMap<String,List>();
        if(player < 1 && player == null ){
            return new Result<>(true, StatusCode.ERROR, "参数错误");
        }else if(player == 1){
            return new Result<>(true, StatusCode.ERROR, "玩家最少为2人");
        }else if(player == 2){
            resultMap.put("player1",new ArrayList());
            resultMap.put("player2",new ArrayList());
            for (int i = 0; i < resultList.size(); i++) {
                if(i%2 == 0){
                    List list = resultMap.get("player1");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player1",list);
                }else {
                    List list = resultMap.get("player2");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player2",list);
                }
            }
        }else if(player == 3){
            resultMap.put("player1",new ArrayList());
            resultMap.put("player2",new ArrayList());
            resultMap.put("player3",new ArrayList());
            for (int i = 0; i < resultList.size(); i++) {
                if(i%3 == 0){
                    List list = resultMap.get("player1");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player1",list);
                }else if(i%3 == 1){
                    List list = resultMap.get("player2");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player2",list);
                }else {
                    List list = resultMap.get("player3");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player3",list);
                }
            }
        }else {
            return new Result<>(true, StatusCode.ERROR, "玩家最多为3人");
        }

        return new Result<>(true, StatusCode.OK, "发牌成功",resultMap);
    }


    // 两副牌开局
    @GetMapping("/start/two/{player}")
    @Parameter(in = ParameterIn.PATH, name = "player", description = "玩家人数", required = true)
    public Result startGameTwo(@PathVariable Integer player) {
        List resultList = randomList(TwoPuke);
        Map<String, List> resultMap = new HashMap<String,List>();
        if(player < 1 && player == null ){
            return new Result<>(true, StatusCode.ERROR, "参数错误");
        }else if(player == 1 && player == 2 && player == 3){
            return new Result<>(true, StatusCode.ERROR, "玩家最少为4人");
        }else if(player == 4){
            resultMap.put("player1",new ArrayList());
            resultMap.put("player2",new ArrayList());
            resultMap.put("player3",new ArrayList());
            resultMap.put("player4",new ArrayList());
            for (int i = 0; i < resultList.size(); i++) {
                if(i%4 == 0){
                    List list = resultMap.get("player1");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player1",list);
                }else if(i%4 == 1){
                    List list = resultMap.get("player2");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player2",list);
                }else if(i%4 == 2){
                    List list = resultMap.get("player3");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player3",list);
                }else {
                    List list = resultMap.get("player4");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player4",list);
                }
            }
        }else if(player == 5){
            resultMap.put("player1",new ArrayList());
            resultMap.put("player2",new ArrayList());
            resultMap.put("player3",new ArrayList());
            resultMap.put("player4",new ArrayList());
            resultMap.put("player5",new ArrayList());
            for (int i = 0; i < resultList.size(); i++) {
                if(i%5 == 0){
                    List list = resultMap.get("player1");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player1",list);
                }else if(i%5 == 1){
                    List list = resultMap.get("player2");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player2",list);
                }else if(i%5 == 2){
                    List list = resultMap.get("player3");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player3",list);
                }else if(i%5 == 3){
                    List list = resultMap.get("player4");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player4",list);
                }else {
                    List list = resultMap.get("player5");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player5",list);
                }
            }
        }else if(player == 6){
            resultMap.put("player1",new ArrayList());
            resultMap.put("player2",new ArrayList());
            resultMap.put("player3",new ArrayList());
            resultMap.put("player4",new ArrayList());
            resultMap.put("player5",new ArrayList());
            resultMap.put("player6",new ArrayList());
            for (int i = 0; i < resultList.size(); i++) {
                if(i%6 == 0){
                    List list = resultMap.get("player1");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player1",list);
                }else if(i%6 == 1){
                    List list = resultMap.get("player2");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player2",list);
                }else if(i%6 == 2){
                    List list = resultMap.get("player3");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player3",list);
                }else if(i%6 == 3){
                    List list = resultMap.get("player4");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player4",list);
                }else if(i%6 == 4){
                    List list = resultMap.get("player5");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player5",list);
                }else {
                    List list = resultMap.get("player6");
                    list.add(resultList.get(i));
                    Collections.sort(list, new Comparator<String>() {
                        public int compare(String a1, String a2) {
                            int io1 = sortRule.indexOf(a1);
                            int io2 = sortRule.indexOf(a2);
                            return io1 - io2;
                        }
                    });
                    resultMap.put("player6",list);
                }
            }
        }else {
            return new Result<>(true, StatusCode.ERROR, "玩家最多为6人");
        }

        return new Result<>(true, StatusCode.OK, "发牌成功",resultMap);
    }

    // 打乱顺序
    public List randomList(List<String> list){
        // 随机数组
        List<Integer> randoms = new ArrayList<Integer>();
        for (int i = 0; i < 1000; i++) {
            randoms.add((int) Math.ceil(Math.random() * list.size()-1));
        }
        // 执行打乱
        for (int i = 0; i < randoms.size(); i++) {
            String l = list.get(0);
            list.set(0,list.get(randoms.get(i)));
            list.set(randoms.get(i),l);
        }
        return list;
    }


}
